package com.aitesting.controller;


import com.aitesting.common.CommonResult;
import com.aitesting.model.Testcase;
import com.aitesting.service.TestcaseService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 测试用例管理
 * @author zz
 * @since 2022-02-20
 */
@Slf4j
@RestController
@RequestMapping("/testcase")
public class TestcaseController {

    @Autowired
    private TestcaseService testcaseService;

    /**
     * 根据id查询测试用例
     * @param id 测试用例id
     * @return CommonResult(code,message,data)
     */
    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    public CommonResult<Testcase> getById(@PathVariable("id") Integer id) {
        return CommonResult.success(testcaseService.getById(id));
    }

    /**
     * 查询所有测试用例
     * @return CommonResult(code,message,data)
     */
    @RequestMapping(value = "/listAll", method = RequestMethod.GET)
    public CommonResult<Testcase> getList() {
        return CommonResult.success(testcaseService.list());
    }

    /**
     * 新增测试用例
     * @param testcase 测试用例对象
     * @return CommonResult(code,message,data)
     */
    @RequestMapping(value = "/create", method = RequestMethod.POST)
    public CommonResult<Testcase> create(@RequestBody Testcase testcase){
        boolean isOk = testcaseService.save(testcase);
        if (isOk) {
            return CommonResult.success(testcase);
        }else {
            log.error("新增测试用例失败,数据不合法 {}", testcase);
            return CommonResult.failed();
        }
    }

    /**
     * 根据id删除测试用例
     * @param id 测试用例id
     * @return CommonResult(code,message,data)
     */
    @RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
    public CommonResult<Testcase> delete(@PathVariable Integer id) {
        boolean isOk = testcaseService.removeById(id);
        if (isOk) {
            return CommonResult.success(null);
        }else {
            return CommonResult.failed();
        }
    }

    /**
     * 根据id更新测试用例
     * @param testcase 测试用例对象
     * @return CommonResult(code,message,data)
     */
    @RequestMapping(value = "/update", method = RequestMethod.PUT)
    public CommonResult<Testcase> update(@RequestBody Testcase testcase) {
        boolean isOk = testcaseService.updateById(testcase);
        if (isOk) {
            return CommonResult.success(testcase);
        }else {
            log.error("更新测试用例失败,数据不合法 {}", testcase);
            return CommonResult.failed();
        }
    }
}

